package com.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.dto.HospitalizationRecordDTO;
import com.hospital.entity.HospitalizationRecord;
import com.hospital.mapper.DepartmentMapper;
import com.hospital.mapper.HospitalizationRecordMapper;
import com.hospital.mapper.PatientMapper;
import com.hospital.result.PageResult;
import com.hospital.service.HospitalizationRecordService;
import com.hospital.vo.AdmissionRecordVO;
import com.hospital.vo.AdmissionTrendVO;
import com.hospital.vo.DischargeRecordVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class HospitalizationRecordServiceImpl extends ServiceImpl<HospitalizationRecordMapper, HospitalizationRecord> implements HospitalizationRecordService {
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private HospitalizationRecordMapper hospitalizationRecordMapper;
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public PageResult pageQueryAdmission(HospitalizationRecordDTO hospitalizationRecordDTO) {
        IPage<AdmissionRecordVO> page = new Page<>(hospitalizationRecordDTO.getPageNumber(), hospitalizationRecordDTO.getPageSize());

        hospitalizationRecordMapper.pageAdmissionRecord(page);

        return PageResult.builder()
                .total(page.getTotal())
                .records(page.getRecords())
                .build();
    }

    @Override
    public PageResult pageQueryDischarge(HospitalizationRecordDTO hospitalizationRecordDTO) {
        IPage<DischargeRecordVO> page = new Page<>(hospitalizationRecordDTO.getPageNumber(), hospitalizationRecordDTO.getPageSize());

        hospitalizationRecordMapper.pageDischargeRecord(page);

        return PageResult.builder()
                .total(page.getTotal())
                .records(page.getRecords())
                .build();
    }

    @Override
    public List<AdmissionTrendVO> getAdmissionTrend() {
        List<AdmissionTrendVO> admissionTrendVOList = new ArrayList<>();
        // 获取前六月的入院人数
        LocalDateTime now = LocalDateTime.now().toLocalDate().atStartOfDay();
        for (int i = 5; i >= 0; i--) {
            LocalDateTime start = now.minusMonths(i).withDayOfMonth(1);
            LocalDateTime end = now.minusMonths(i - 1).withDayOfMonth(1).minusDays(1);
            int count = hospitalizationRecordMapper.selectList(
                    new LambdaQueryWrapper<HospitalizationRecord>()
                            .ge(HospitalizationRecord::getAdmissionTime, start)
                            .le(HospitalizationRecord::getAdmissionTime, end)
            ).size();
            admissionTrendVOList.add(AdmissionTrendVO.builder()
                    .date(start)
                    .count(count)
                    .build());
        }

        return admissionTrendVOList;
    }
}
